package com.hdjk.view.activity.personalcenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.hdjk.view.R;
import com.hdjk.view.adapter.BuyPlanListAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Constant;
import com.hdjk.view.entity.BuyPlanEntity;
import com.hdjk.view.network.PlanNetWork;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.loadingviewfinal.DefaultLoadMoreView;
import cn.finalteam.loadingviewfinal.ListViewFinal;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;

/**
 * 定投计划列表列表
 * Created by sxy on 2017/11/10.
 */

public class BuyPlanListActivity extends BaseActivity implements View.OnClickListener {
    private boolean isRefresh = false;
    private List<BuyPlanEntity> mList;
    private BuyPlanListAdapter mAdapter;
    private int mPageIndex = 0;
    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private PtrClassicFrameLayout ptr_layout;
    private ListViewFinal lv_data_list;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        ptr_layout = (PtrClassicFrameLayout) findViewById(R.id.ptr_layout);
        lv_data_list = (ListViewFinal) findViewById(R.id.lv_data_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_buy_plan_list);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        IntentFilter filter = new IntentFilter(Constant.INTENT_FILTER_STRING_AFTER_BUY_PLAN_MODIFY_STATE);
        registerReceiver(mReceiver,filter);
    }

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constant.INTENT_FILTER_STRING_AFTER_BUY_PLAN_MODIFY_STATE)) {
                isRefresh = true;
            }
        }
    };

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new BuyPlanListAdapter(this);
        mAdapter.setDataList(mList);
        lv_data_list.setLoadMoreView(new DefaultLoadMoreView(this));
        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = (int) id;
                Intent intent = new Intent(BuyPlanListActivity.this, BuyPlanDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("entity", mList.get(index));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        ptr_layout.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                getBuyPlanList(true);
            }
        });
        ptr_layout.setLastUpdateTimeRelateObject(this);
        lv_data_list.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                // 上拉加载
                getBuyPlanList(false);
            }
        });
        getBuyPlanList(true);
    }

    private void onLoaded() {
        ptr_layout.onRefreshComplete();
        lv_data_list.onLoadMoreComplete();
    }

    private void getBuyPlanList(final boolean isDropDown) {
        mDialog.show();
        if (isDropDown) {
            mPageIndex = 1;
        }
        JSONObject obj = new JSONObject();
        try {
            obj.put("accountNumber", UserTokenUtil.getUserName(BuyPlanListActivity.this));
            obj.put("pageIndex", mPageIndex);
            obj.put("pageSize", 10);
            PlanNetWork.getBuyPlanList(UserTokenUtil.getToken(BuyPlanListActivity.this), obj.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    onLoaded();
                    mDialog.dismiss();
                    isRefresh=false;
                    if (msg != null && msg.getSuccess() && msg.getData() != null) {
                        List<BuyPlanEntity> tempList = (ArrayList) msg.getData();
                        if (null != tempList) {
                            mPageIndex += 1;
                            if (isDropDown) {
                                mList = tempList;
                            } else {
                                mList.addAll(tempList);
                            }

                            if (mAdapter == null) {
                                mAdapter = new BuyPlanListAdapter(BuyPlanListActivity.this);
                                mAdapter.setDataList(mList);
                                lv_data_list.setAdapter(mAdapter);
                            } else {
                                mAdapter.setDataList(mList);
                                mAdapter.notifyDataSetChanged();
                            }
                            lv_data_list.setHasLoadMore(true);
                        }
                        if (null == tempList || (tempList.size() >= 0 && tempList.size() < 10)) {
                            lv_data_list.setHasLoadMore(false);
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            getBuyPlanList(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
}
